﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using SCRUM.Core.BusinessObjects;
using System.Data.SqlClient;
using System.Data;

namespace SCRUM.Core.MembershipProviders
{
    /// <summary>
    /// Role provider for comunication with the database
    /// </summary>
    public class RoleDBDataProvider : RoleDataProvider
    {
        /// <summary>
        /// Gets users acording to their role and team
        /// </summary>
        /// <param name="roleName">desired role</param>
        /// <param name="teamID">ID of the team</param>
        /// <returns>list of users</returns>
        public override List<User> GetUsersInRoleInTeam(string roleName, int teamID)
        {
            if (string.IsNullOrEmpty(roleName)) throw new ArgumentNullException("rolename");
            roleName = roleName.ToLower();
            //if (teamID neni ok) throw new ArgumentNullException("teamID");

            DbParameter dbroleName = MembershipDBDataProvider.CreateParameter("roleName", roleName);
            DbParameter dbteamID = MembershipDBDataProvider.CreateParameter("teamID", teamID);

            List<User> users = new List<User>();

            try
            {
                MembershipDBDataProvider.connection.Open();
                //DataTable roleTable = new DataTable();

                DbDataReader r = MembershipDBDataProvider.GetGrid("role/GetUsersInRoleInTeam", dbroleName, dbteamID);

                while (r.NextResult())
                {
                    //User u = getus
                }

/*
                SqlCommand cmd = new SqlCommand("SELECT UserName FROM UsersInRoles WHERE RoleName=@RoleName AND UserName LIKE @UserName");
                
                cmd.Parameters.Add("@RoleName", SqlDbType.VarChar, 100).Value = rolename;
                cmd.Parameters.Add("@UserName", SqlDbType.VarChar, 100).Value = usernameToMatch;
                using (SqlDataAdapter da = new SqlDataAdapter(cmd)) da.Fill(roleTable);
                return //TableToArray(roleTable);
*/
                return users;
            }
            finally
            {
                MembershipDBDataProvider.connection.Close();
            }
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets all roles.
        /// </summary>
        /// <returns></returns>
        public override List<Role> GetRoles(bool teamRolesOnly)
        {
            List<Role> roles = new List<Role>();

            try
            {
                MembershipDBDataProvider.connection.Open();


                DbParameter dbTeamRolesOnly = MembershipDBDataProvider.CreateParameter("teamRolesOnly", teamRolesOnly);


                DbDataReader data = MembershipDBDataProvider.GetGrid("role/GetAllRolesAsBO", dbTeamRolesOnly);

                while (data.Read())
                {
                    Role role = new Role(
                        (int)data["ID"],
                        (string)data["NAME"]
                    );
                    roles.Add(role);
                }
            }
            finally
            {
                MembershipDBDataProvider.connection.Close();
            }
            return roles;
        }

        public override string[] GetRolesForUser(string username)
        {
            throw new NotImplementedException();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            throw new NotImplementedException();
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }

        #region Unused methods

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }



        #endregion

        /// <summary>
        /// Gets the role by ID.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public override Role GetRoleByID(int id)
        {
            Role role = null;
            try
            {
                MembershipDBDataProvider.connection.Open();
                DbParameter roleID = MembershipDBDataProvider.CreateParameter("roleID", id);

                DbDataReader data = MembershipDBDataProvider.GetGrid("role/GetRoleByID", roleID);

                if (data.Read())
                {
                    role = new Role(
                        (int)data["ID"],
                        (string)data["NAME"]
                    );
                }
            }
            finally
            {
                MembershipDBDataProvider.connection.Close();
            }
            return role;
        }

        /// <summary>
        /// Gets the role for user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns>Role for the specified user.</returns>
        public override Role GetRoleForUser(User user)
        {
            Role role = null;
            try
            {
                MembershipDBDataProvider.connection.Open();
                DbParameter userID = MembershipDBDataProvider.CreateParameter("userID", user.ID);

                DbDataReader data = MembershipDBDataProvider.GetGrid("role/GetRoleForUser", userID);

                if (data.Read())
                {
                    role = new Role(
                        (int)data["ID"],
                        (string)data["NAME"]
                    );
                }
            }
            finally
            {
                MembershipDBDataProvider.connection.Close();
            }
            return role;
        }
    }
}